package com.yc.d0416;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class QQClient {
    public static void main(String[] args) throws IOException {

        //创建即连接
        Socket socket = new Socket("127.0.0.1",8888);

        final InputStream in = socket.getInputStream();
        final OutputStream out = socket.getOutputStream();


        Scanner sc = new Scanner(System.in);

        Thread t1 = new Thread(()->{
            while (true){
                try {
                    System.out.print("请输入 1:发送消息。2:发送文件");
                    final int i = Integer.parseInt(sc.nextLine());
                    if (i == 1 ){
                        String msg = sc.nextLine();
                        MsgUtil.sendText(msg, out);
                    }else if(i == 2){
                        System.out.println("请输入文件路径:");
                        String filepath = sc.nextLine();
                        MsgUtil.sendFile(filepath, out);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(()->{
            while (true){
                try {
                    String msg = MsgUtil.recv(in);
                    System.out.println("服务器:" + msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();
    }
}
